package com.ruoyi.controller.basic;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.domain.basic.MesWorkstation;
import com.ruoyi.service.basic.IMesWorkstationService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 工位信息Controller
 * 
 * @author ruoyi
 * @date 2024-01-01
 */
@RestController
@RequestMapping("/mes/workstation")
public class MesWorkstationController extends BaseController {
    @Autowired
    private IMesWorkstationService mesWorkstationService;

    /**
     * 查询工位信息列表
     */
    @PreAuthorize("@ss.hasPermi('mes:workstation:list')")
    @GetMapping("/list")
    public TableDataInfo list(MesWorkstation mesWorkstation) {
        startPage();
        List<MesWorkstation> list = mesWorkstationService.selectMesWorkstationList(mesWorkstation);
        return getDataTable(list);
    }

    /**
     * 导出工位信息列表
     */
    @PreAuthorize("@ss.hasPermi('mes:workstation:export')")
    @Log(title = "工位信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MesWorkstation mesWorkstation) {
        List<MesWorkstation> list = mesWorkstationService.selectMesWorkstationList(mesWorkstation);
        ExcelUtil<MesWorkstation> util = new ExcelUtil<MesWorkstation>(MesWorkstation.class);
        util.exportExcel(response, list, "工位信息数据");
    }

    /**
     * 获取工位信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('mes:workstation:query')")
    @GetMapping(value = "/{workstationId}")
    public AjaxResult getInfo(@PathVariable("workstationId") Long workstationId) {
        return success(mesWorkstationService.selectMesWorkstationByWorkstationId(workstationId));
    }

    /**
     * 新增工位信息
     */
    @PreAuthorize("@ss.hasPermi('mes:workstation:add')")
    @Log(title = "工位信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MesWorkstation mesWorkstation) {
        return toAjax(mesWorkstationService.insertMesWorkstation(mesWorkstation));
    }

    /**
     * 修改工位信息
     */
    @PreAuthorize("@ss.hasPermi('mes:workstation:edit')")
    @Log(title = "工位信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MesWorkstation mesWorkstation) {
        return toAjax(mesWorkstationService.updateMesWorkstation(mesWorkstation));
    }

    /**
     * 删除工位信息
     */
    @PreAuthorize("@ss.hasPermi('mes:workstation:remove')")
    @Log(title = "工位信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{workstationIds}")
    public AjaxResult remove(@PathVariable Long[] workstationIds) {
        return toAjax(mesWorkstationService.deleteMesWorkstationByWorkstationIds(workstationIds));
    }

    /**
     * 校验工位编码
     */
    @PostMapping("/checkWorkstationCodeUnique")
    public AjaxResult checkWorkstationCodeUnique(@RequestBody MesWorkstation mesWorkstation) {
        String result = mesWorkstationService.checkWorkstationCodeUnique(mesWorkstation);
        if ("0".equals(result)) {
            return success();
        }
        return error("工位编码已存在");
    }

    /**
     * 查询启用状态的工位列表
     */
    @GetMapping("/getActiveList")
    public AjaxResult getActiveList() {
        List<MesWorkstation> list = mesWorkstationService.selectActiveWorkstationList();
        return success(list);
    }

    /**
     * 根据车间ID查询工位列表
     */
    @GetMapping("/getByWorkshop/{workshopId}")
    public AjaxResult getByWorkshop(@PathVariable Long workshopId) {
        List<MesWorkstation> list = mesWorkstationService.selectMesWorkstationByWorkshopId(workshopId);
        return success(list);
    }
}